package com.pi.pro.xcmg.am.was.fac.handler;

import com.pi.framework.core.util.StringUtils;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.TwaDocSoHeader;
import com.pi.wms.entity.TwtActPalletizeList;
import com.pi.wms.fac.CustomPredicateQuery;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class SoCustomPredicate extends CustomPredicateQuery<TwaDocSoHeader> {

    @Override
    public void toPredicate(Root<TwaDocSoHeader> root, List<Predicate> predicates, CriteriaQuery<?> criteriaQuery, CriteriaBuilder builder, Map<String, ?> query) {
        if (!this.validate(root, predicates, criteriaQuery, builder, query)){
            return;
        }
        String refdocnum = (String) query.get("refdocnum");
        if (!StringUtils.isNullOrBlank(refdocnum)) {
            predicates.add(builder.or(new Predicate[]{builder.like(root.get("refDocNumber"), "%" + refdocnum.trim() + "%")}));
        }
        String refasnnum = (String) query.get("refasnnum");
        if (!StringUtils.isNullOrBlank(refasnnum)) {
            predicates.add(builder.or(new Predicate[]{builder.like(root.get("refAsnNumber"), "%" + refasnnum.trim() + "%")}));
        }
        String docnum = (String) query.get("docnum");
        if (!StringUtils.isNullOrBlank(docnum)) {
            predicates.add(builder.or(new Predicate[]{builder.like(root.get("documentNumber"), "%" + docnum.trim() + "%")}));
        }
        String workstatus = (String) query.get("workstatus");
        if (!StringUtils.isNullOrBlank(workstatus)) {
            workstatus=workstatus.trim();
            if ("0000".equalsIgnoreCase(workstatus)){
                predicates.add(builder.equal(root.get("status"), workstatus));
            }else if ("0100".equalsIgnoreCase(workstatus)){
                predicates.add(builder.equal(root.get("status"), WmsConstants.SO_STATUS_CREATE));
                predicates.add(builder.isNotNull(root.get("waveId")));
            }else if ("0850".equalsIgnoreCase(workstatus)){
                ((Map)query).put("option", "SHOWALL");
                ((Map)query).put("status", workstatus);
                predicates.add(builder.equal(root.get("status"), workstatus));
            }else if ("0880".equalsIgnoreCase(workstatus)){
                ((Map)query).put("option", "SHOWALL");
                ((Map)query).put("status", workstatus);
                //predicates.add(builder.equal(root.get("status"), workstatus));
            }else {
                predicates.add(builder.equal(root.get("status"), workstatus));
            }
        }
    }
}
